package handlers

import (
	"github.com/gin-gonic/gin"
	"sh/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	author := c.PostForm("author")

	data := models.ArticleSh{
		Title:   title,
		Content: content,
		Author:  author,
	}

	err := models.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func List(c *gin.Context) {
	var articles []models.ArticleSh
	models.DB.Debug().Find(&articles)
	c.JSON(200, gin.H{
		"code": 200,
		"data": articles,
		"msg":  "查询成功",
	})
}
